//
//  ByDoubleBallDragTableView.m
//  caipiao_ios
//
//  Created by mc on 2018/7/18.
//  Copyright © 2018年 ZhangZhanbin. All rights reserved.
//

#import "ByDoubleBallDragTableView.h"
#import "BySuperSectionView.h"
#import "BySomeCirclesView.h"
#import "BySuperHandleViews.h"
#import "ByLotteryAlgorithmHandle.h"
#import "ByDottedLineView.h"




@implementation ByDoubleBallDragTableView

- (NSMutableArray *)redOneArray{
    if (_redOneArray == nil) {
        _redOneArray = [NSMutableArray arrayWithCapacity:1];
    }
    return _redOneArray;
}

- (NSMutableArray *)redTwoArray{
    if (_redTwoArray == nil) {
        _redTwoArray = [NSMutableArray arrayWithCapacity:1];
    }
    return _redTwoArray;
}

- (NSMutableArray *)blueOneArray{
    if (_blueOneArray == nil) {
        _blueOneArray = [NSMutableArray arrayWithCapacity:1];
    }
    return _blueOneArray;
}





- (id)init{
    self = [super init];
    if (self) {
        
        self.backgroundColor = BY_BACKGROUND_COLOR;
        self.delegate = self;
        self.dataSource = self;
        self.separatorStyle = UITableViewCellSeparatorStyleNone;
        self.estimatedRowHeight = 0;
        self.estimatedSectionHeaderHeight = 0;
        self.estimatedSectionFooterHeight = 0;
        [self loadRandomData];
    }
    return self;
}

- (void)loadRandomData{
    self.redOneArray = [ByLotteryAlgorithmHandle getOriginalDataWithCount:33];
    self.blueOneArray = [ByLotteryAlgorithmHandle getOriginalDataWithCount:16];
    self.redTwoArray = [ByLotteryAlgorithmHandle getOriginalDataWithCount:33];
    [self reloadData];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    static NSString *cellIdentifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        
        UILabel *line = [[UILabel alloc] init];
        [cell addSubview:line];
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(cell).offset(0);
            make.right.equalTo(cell).offset(0);
            make.bottom.equalTo(cell).offset(0);
            make.height.mas_equalTo(@1);
        }];
        line.backgroundColor = BY_LINE_COLOR;
    }
    
    for (UIView *view in cell.contentView.subviews) {
        [view removeFromSuperview];
    }
    
    cell.contentView.backgroundColor = BY_BACKGROUND_COLOR;
    
    UIView *backgroundView = [[UIView alloc] init];
    [cell.contentView addSubview:backgroundView];
    [backgroundView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(cell.contentView).offset(5);
        make.right.equalTo(cell.contentView).offset(-5);
        make.top.equalTo(cell.contentView).offset(0);
        make.bottom.equalTo(cell.contentView).offset(0);
    }];
    backgroundView.layer.cornerRadius = 5;
    backgroundView.layer.masksToBounds = YES;
    backgroundView.backgroundColor = [UIColor whiteColor];
    
    BySuperSectionView *sectionView = [[BySuperSectionView alloc] init];
    [backgroundView addSubview:sectionView];
    [sectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(backgroundView).offset(10);
        make.top.equalTo(backgroundView).offset(10);
        make.right.equalTo(backgroundView).offset(0);
        make.height.mas_equalTo(@20);
    }];
    sectionView.titleLabel.text = @"胆码:红球";
    [sectionView.titleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(60, 20));
    }];
    sectionView.noteLabel.text = @"最少选择1个，最多选5个";
    sectionView.selectBlock= ^(){
        ByWebViewController *vc = [[ByWebViewController alloc] initWithTotalUrlString:[NSString stringWithFormat:@"%@/Wanfajiesao/jiesao?type=1001",BOYE_WEB_BASE_URL]];
        vc.titleString = @"双色球玩法说明";
        [(ByBaseVC *)self.viewController by_normalPush:vc hasNavi:YES];
    };
    
    
    BySomeCirclesView *redOneView = [[BySomeCirclesView alloc] init];
    [backgroundView addSubview:redOneView];
    [redOneView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(backgroundView).offset(0);
        make.right.equalTo(backgroundView).offset(0);
        make.top.equalTo(sectionView.mas_bottom).offset(10);
        make.height.mas_equalTo(@([BySomeCirclesView getHeighWithItemsAry:[self.redOneArray copy]]));
    }];
    redOneView.themeColor = BY_THEME_COLOR;
    redOneView.itemsAry = [self.redOneArray copy];
    redOneView.selectBlock = ^(NSInteger index){
        NSArray *selectedAry = [ByLotteryAlgorithmHandle getSelectedDataWithArray:[self.redOneArray copy]];
        ByNetModel *netModel = [self.redOneArray objectAtIndex:index];
        for (ByNetModel *model in self.redTwoArray) {
            if (model.value == netModel.value && model.selected) {
                model.selected = NO;
                [self reloadData];
            }
        }
        if (selectedAry.count > 4 && !netModel.selected) {
            [ByCoreToolAlert showOnlyText:@"胆码:红球(前区)最多选5个"];
            return ;
        }
        
        netModel.selected = !netModel.selected;
        
        [self reloadData];
        if (self.updateBlock) {
            self.updateBlock();
        }
    };
    
    
    
    BySuperSectionView *sectionView1 = [[BySuperSectionView alloc] init];
    [backgroundView addSubview:sectionView1];
    [sectionView1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(backgroundView).offset(10);
        make.top.equalTo(redOneView.mas_bottom).offset(10);
        make.right.equalTo(backgroundView).offset(0);
        make.height.mas_equalTo(@20);
    }];
    sectionView1.playView.hidden = YES;
    sectionView1.noteLabel.text = @"最少选择2个";
    
    sectionView1.titleLabel.text = @"拖码:红球";
    [sectionView1.titleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(60, 20));
    }];
    
    BySomeCirclesView *redTwoView = [[BySomeCirclesView alloc] init];
    [backgroundView addSubview:redTwoView];
    [redTwoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(backgroundView).offset(0);
        make.right.equalTo(backgroundView).offset(0);
        make.top.equalTo(sectionView1.mas_bottom).offset(10);
        make.height.mas_equalTo(@([BySomeCirclesView getHeighWithItemsAry:[self.redTwoArray copy]]));
    }];
    redTwoView.themeColor = BY_THEME_COLOR;
    redTwoView.itemsAry = [self.redTwoArray copy];
    redTwoView.selectBlock = ^(NSInteger index){
        ByNetModel *netModel = [self.redTwoArray objectAtIndex:index];
        netModel.selected = !netModel.selected;
        for (ByNetModel *model in self.redOneArray) {
            if (model.value == netModel.value && netModel.selected && model.selected) {
                model.selected = NO;
            }
        }
        [self reloadData];
        if (self.updateBlock) {
            self.updateBlock();
        }
    };
    
    
    
    BySuperSectionView *sectionView2 = [[BySuperSectionView alloc] init];
    [backgroundView addSubview:sectionView2];
    [sectionView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(backgroundView).offset(10);
        make.top.equalTo(redTwoView.mas_bottom).offset(10);
        make.right.equalTo(backgroundView).offset(0);
        make.height.mas_equalTo(@20);
    }];
    sectionView2.playView.hidden = YES;
    sectionView2.noteLabel.text = @"最少选择1个";
    sectionView2.titleLabel.textColor = BY_Blue_COLOR;
    sectionView2.titleLabel.text = @"篮球";
    [sectionView2.titleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(30, 20));
    }];
    
    BySomeCirclesView *blueOneView = [[BySomeCirclesView alloc] init];
    [backgroundView addSubview:blueOneView];
    [blueOneView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(backgroundView).offset(0);
        make.right.equalTo(backgroundView).offset(0);
        make.top.equalTo(sectionView2.mas_bottom).offset(10);
        make.height.mas_equalTo(@([BySomeCirclesView getHeighWithItemsAry:[self.blueOneArray copy]]));
    }];
    blueOneView.themeColor = BY_Blue_COLOR;
    blueOneView.itemsAry = [self.blueOneArray copy];
    blueOneView.selectBlock = ^(NSInteger index){
        ByNetModel *netModel = [self.blueOneArray objectAtIndex:index];
        netModel.selected = !netModel.selected;
        [self reloadData];
        if (self.updateBlock) {
            self.updateBlock();
        }
    };
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return [BySomeCirclesView getHeighWithItemsAry:self.redOneArray] * 2 + [BySomeCirclesView getHeighWithItemsAry:self.blueOneArray] + 40 * 3;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}

@end
